package org.grow.cooldown.ScoreModule;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Random;

@Slf4j
@Service

public class CommonCookieIntercepter implements HandlerInterceptor{
    private static ThreadLocal<Integer> i = new ThreadLocal<>();

    Random random = new Random();

    private void debug(HttpServletRequest request, HttpServletResponse response){
        if (request.getCookies()!=null) {
            for (int i = 0; i < request.getCookies().length; i++) {
                System.out.println(request.getCookies()[i].getName()+
                        " ==>> "+request.getCookies()[i].getValue());
            }
        }
//        while (request.getHeaderNames().hasMoreElements()) {
//            Object nextElement =  request.getHeaderNames().nextElement();
//            System.out.println(nextElement);
//        }
        log.info(response.getHeaderNames().toString());
    }
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        log.info("Pre");
        debug(request,response);
        var f = -1;
        Cookie[] cookies = request.getCookies();
        if (cookies == null ) cookies=new Cookie[]{};
        for (var i = 0; i < cookies.length; i++) {

            if (cookies[i].getName().equals("laborMedal")){
                System.out.println("=====----您已经是有劳动奖章的好人了好人奖章，欢迎再次游览");
                f =i;
                break;
            }
        }

        if (f==-1){
            //如果没有奖章，就送他一枚奖章，下次可以用
            System.out.println("似乎你是第一次来，没有劳动奖章，那么请交50元，我们给你一个才能游览");
            Cookie laborMedal = new Cookie("laborMedal", String.format("Labor_%04d", random.nextInt(4000)));

            laborMedal.setMaxAge(4200);
            System.out.println(laborMedal.getName()+"cookie :: manu ::" + laborMedal.getValue());
            response.addCookie(laborMedal);

        }else {
            //如果有奖章 就公布一下好人奖章，并且去查数据库看看奖章是否真实
            log.info("from request cookie value : ",cookies[f].toString());
            log.info("from redis cookie value: ");
        }
        i.set(f);

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        var f = i.get();

        if (f==-1) {

            log.info("Post");
            debug(request, response);
            System.out.println("=====----您是第一次来，不过游览了一遍，觉得风景如何");
        }else{
            System.out.println("=====----您已经游览过好多次了吧，谢谢支持-----=====");
        }
        i.remove();
    }
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        log.info("After");
        debug(request,response);
    }
}
